import time
from masoniteorm.relationships import belongs_to

from application.config import appSettings
from ..base import BaseModel


class Order(BaseModel):
    __connection__ = 'postgres'

    __table__ = appSettings.db_code_prefix + 'orders'

    __casts__ = {
        'big_status': 'int',
        'big_diy_status': 'int',
        'middle_status': 'int',
        'middle_diy_status': 'int',
        'small_status': 'int',
        'small_diy_status': 'int',
        'serial_status': 'int',
        'serial_diy_status': 'int',
        'security_status': 'int',
        'verify_status': 'int',
    }

    def gen_order_sn(self):
        return str(time.strftime('%Y%m%d%H%M%S', time.localtime(time.time()))) + str(time.time()).replace('.', '')[-2:]

    def create_order_sn(self):
        order_sn = self.gen_order_sn()
        while self.where('order_sn', order_sn).first():
            order_sn = self.gen_order_sn()
        return order_sn

    @belongs_to('company_code', 'code')
    def brand(self):
        from ..oem.Company import Company
        return Company

    @belongs_to('good_code', 'good_code')
    def good(self):
        from ..oem.Good import Good
        return Good


__all__ = ['Order']
